import 'dart:developer';

import 'package:get/get.dart';
import 'package:ball_lion/entity/expert/plan_pack/plan_pack_entity.dart';
import 'package:ball_lion/server/api/expert_api.dart';

import 'package:ball_lion/entity/pay/recharge_entity.dart';
import 'package:ball_lion/server/api/pay_api.dart';
import 'package:ball_lion/utils/user.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/module/pay/pay_bottom_sheet.dart';

class PlanPackDetailLogic extends GetxController {
  int? packId = Get.arguments;
  PlanPackEntity? data;

  @override
  void onReady() {
    getData();
    super.onReady();
  }

  Future getData() async {
    data = await ExpertApi.getPlanPackDetail(packId ?? 0);
    update();
  }

  Future toPay() async {
    final price = data?.price;
    if (price == null) {
      return;
    }
    final gold = double.tryParse(price);
    if (gold == null) {
      return;
    }
    final PayResult? result = await PayBottomSheet.show(
        payInfo: PayInfo(
            price: price,
            payScene: PayScene.planPack,
            buyId: "${data?.packageId}"));
    if (result != null && result.toPay == PayStatus.goldPay) {
      log("--------goldPay--------");
      var s = await PayApi.planPackOrder(data?.packageId);
      if (s == true) {
        Utils.onEvent('pay_done');
        await getData();
      } else {
        Utils.onEvent('fatcxq_zfyd_qxzf');
      }
    } else if (result?.toPay == PayStatus.directPay) {
      log("--------directPay--------");
      Utils.onEvent('fatcxq_zfyd_qrzf');
      await getData();
    } else {
      Utils.onEvent('fatcxq_zfyd_qxzf');
    }
    User.fetchUserInfo(fetchFocus: false);
  }
}
